import React from "react";
import routeWithUserSession from "@/components/ApplicationArea/routeWithUserSession";
import PageHeader from "@/components/PageHeader";
import { ControllerType } from "@/components/items-list/ItemsList";
import { BrowserRouter as Router,Link } from 'react-router-dom';//
import {Layout, Button, Col, Menu, Divider, Form, Input, Row, Table, ConfigProvider, Icon, Modal, Tree} from "antd/lib";
import zhCN from 'antd/es/locale/zh_CN';
import Workflow from "@/services/workflow";

import {createBrowserHistory} from 'history'
import notification from "@/services/notification";
import {get} from "lodash";
import Enums from "@/services/enums";
import CreateTemplatesDialog from "@/components/contract/settings/TemplatesDialog";
import navigateTo from "@/components/ApplicationArea/navigateTo";
import {currentUser} from "@/services/auth";
import CheckDialog from "@/components/workflow/CheckDialog";
import routes from "@/services/routes";
import Contracts from "@/services/contracts";
import "@/pages/contracts/Contract.css";
import {WarningOutlined,SecurityScanTwoTone,DownOutlined} from "@ant-design/icons";
// import Templates from "@/services/templates";
const customHistory = createBrowserHistory();
//const HashHistory = createHashHistory();
//const history = useHistory();
const {Sider } = Layout;
const { confirm } = Modal;
const getRowClassName = (record, index) => {
  let className = '';
  if (index % 2 === 0) className = "oddRow";
  return className
}
export default class HomeTableList extends React.Component {
  state = {
    page: 1,
    pagesize: 15,
    total:15,
    loading: false,
    statename: '',
    data: [],
    gData: [],
    selectedKeys: '',
    selectedTitle: '',
    seltype: "1",
  };
  componentWillMount() {
    let param={
      page: this.state.page,
      pagesize: this.state.pagesize,
      statename: this.state.statename,
      seltype: this.props.seltype,
      bill_enum: this.state.selectedKeys,
      userid:  currentUser.id,
    }
    this.fetch(param);
  }
  fetch = (params = {}) => {
    this.setState({ loading: true });
    if(params.seltype==="1"){
      Workflow.getchecklist(params).then(data => {
        this.setState({
            loading: false,
            data:data.data,
            page: params.page,
            pagesize: params.pagesize,
            total: data.total,
            selectedKeys: params.bill_enum,
            seltype: params.seltype,
        });
      });
    }else{
      Contracts.getlist(params).then(data => {
        this.setState({
            loading: false,
            data:data.data,
            page: params.page,
            pagesize: params.pagesize,
            total: data.total,
            seltype: params.seltype,
        });
    });
    }
  };

   handleTableChange = (pagination, filters, sorter) => {
    let field=sorter.field;
    let order=0;
    if(sorter.order==="descend"){
      order=1;
    }
    let param={
      field: field,
      order: order,
      page: pagination.current,
      pagesize: this.state.pagesize,
      statename: this.state.statename,
      seltype: this.state.seltype,
      bill_enum: this.state.selectedKeys,
      userid:  currentUser.id,
      code: "",
      name: "",
      party_a: "",
      type: "",
      id:0,
      departmentid: [],
      amount:0,
      amountmax:0,
      datadown: false,
      warns: "0",
    }
    this.fetch(param);
  };

  tabsClick = e => {
    let param={
      page: 1,
      pagesize: this.state.pagesize,
      statename: this.state.statename,
      seltype: e.key,
      bill_enum: this.state.selectedKeys,
      userid:  currentUser.id,
      code: "",
      name: "",
      party_a: "",
      type: "",
      id:0,
      departmentid: [],
      amount:0,
      amountmax:0,
      datadown: false,
      warns: "0",
    }
    this.fetch(param);
  };
onDragEnter = info => {
    console.log(info);
  };
  render() {
    // const { currentPage } = this.state;
      const paginationProps = {
            showSizeChanger: true,
            // showQuickJumper: true,
            total: this.state.total, // 数据总数
            pageSize: parseInt(this.state.pagesize), // 每页条数
            current: parseInt(this.state.page), // 当前页码
            pageSizeOptions: ["10", "20", "30","100"],
            showTotal: ((total) => {
              return `共 ${total} 条`;
            }),
          };
    const listcolumns = [
      {
        title: '合同预编码',
        key: 'code',
        dataIndex: 'code',
        render: (text, record) => (
          <span>
           {(record.bill_enum ==="contracts") && (
             <Router  history={customHistory}>
              <Link to={`/contract/look/${record.billid}`}>{text}</Link>
             </Router>
           )}
          </span>
        ),
        sorter: (a, b) => a.code > b.code ? 1 : -1
      },
      {
        title: '合同名称',
        dataIndex: 'name',
        key: 'name',
        sorter: (a, b) => a.name > b.name ? 1 : -1
      },
      {
        title: '合同起草日期',
        dataIndex: 'bill_dateformat',
        key: 'bill_dateformat',
        sorter: (a, b) => a.bill_dateformat > b.bill_dateformat ? 1 : -1
      },
      {
        title: '流程状态',
        dataIndex: 'statename',
        key: 'statename',
      },
      {
        title: '状态',
        key: 'status',
        dataIndex: 'status',
        render: (text, record) => (
          <span>
             <p className="clickable">{this.state.seltype ==="1" ? "待审核":"驳回"}</p>
          </span>
        ),
      },
    ];
    const listcolumns2 = [
      {
        title: '合同预编码',
        key: 'code',
        dataIndex: 'code',
        render: (text, record) => (
          <span>
             <Router  history={customHistory}>
              <Link to={`/contract/look/${record.id}`}>{text}</Link>
             </Router>
          </span>
        ),
        sorter: (a, b) => a.code > b.code ? 1 : -1
      },
      {
        title: '合同名称',
        dataIndex: 'name',
        key: 'name',
        sorter: (a, b) => a.name > b.name ? 1 : -1
      },
      {
        title: '合同起草日期',
        dataIndex: 'bill_dateformat',
        key: 'bill_dateformat',
        sorter: (a, b) => a.bill_dateformat > b.bill_dateformat ? 1 : -1
      },
      {
        title: '流程状态',
        dataIndex: 'staycheck',
        key: 'staycheck',
      },
      {
        title: '状态',
        key: 'strstatus',
        dataIndex: 'strstatus',
      },
    ];
    return (
  <ConfigProvider locale={zhCN}>
      <Menu onClick={this.tabsClick} selectedKeys={this.state.seltype} mode="horizontal">
        <Menu.Item key = "1">{"待审核的合同"}</Menu.Item>
        <Menu.Item key = "2">{"提交的合同"}</Menu.Item>
      </Menu>
        <div>
              <Table rowKey='id'
                     columns={this.state.seltype==="1" ? listcolumns:listcolumns2}
                     rowClassName={(record, index) => getRowClassName(record, index)}
                     scroll={{x:'max-content'}}
                     dataSource={this.state.data}
                     pagination={paginationProps}
                     loading={this.state.loading}
                     onChange={this.handleTableChange}
              />
        </div> 
  </ConfigProvider>
    );
  }
}